/**
 * created by zhoulli on 2018/7/10
 * 根据报表类型进行跳转
 */
import React from 'react';
import { connect } from 'react-redux';
import menuRoute from 'routes/menuRoute';
import RLService from 'containers/data-center/report/report-list.service';
import 'styles/data-center/report/report.scss';
import { messages } from "share/common";
import ResizeTable from 'components/resize-table'
import { message } from "antd";

class ReportList extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: true,
      columns: [
        {
          title: messages('data-center.key1'/*序号*/),
          key: "_index",
          dataIndex: '_index',
          width: '10%',
        },
        {
          title: messages('data-center.key2'/*数据表*/),
          key: "reportName",
          dataIndex: 'reportName',
          width: '40%',
        },
        {
          title: messages('data-center.key3'/*描述*/),
          key: "reportDesc",
          dataIndex: 'reportDesc',
          width: '50%',
        },
      ],
      pagination: {
        page: 0,
        total: 0,
        size: 10,
      },
      //报表动态获取
      reportList: [],
    }
  }

  componentDidMount() {
    //记住页码
    let _pagination = this.getBeforePage('ReportList');
    let pagination = this.state.pagination;
    pagination.page = _pagination.page;
    pagination.current = _pagination.page + 1;
    this.setState({
      pagination,
    }, () => {
      this.getReportList();
    })
  }

  //获取报表类型
  getReportList() {
    this.setState({
      loading: true
    });
    let { pagination } = this.state;
    let params = {
      "page": pagination.page,
      "size": pagination.size,
    };
    RLService.getReportList(params)
      .then((res) => {
        let data = res.data;
        //添加一个序号
        data.map((item, index) => item._index = (index + 1) + params.page * params.size);
        pagination.total = Number(res.headers['x-total-count']);
        pagination.current = pagination.page + 1;
        this.setState({
          pagination,
          loading: false,
          reportList: data,
        })
      })
  }

  //跳转报表详情
  goToReportDetail = (item) => {
    this.setBeforePage(this.state.pagination, 'ReportList');
    switch (item.reportType) {
      case 'O': {
        //汇联易应用其他模块的报表
        this.context.router.push("/" + item.reportUrl);
        break;
      }
      case 'MSD':
        //第三方自定义报表
        let path = menuRoute.getRouteItem('third-report', 'key').url.replace(':url', item.id);
        path = path.replace(":type", "MSD");
        this.context.router.push(path);
        break;
      case 'SD': {
        //第三方自定义报表
        let path = menuRoute.getRouteItem('third-report', 'key').url.replace(':url', item.id);
        path = path.replace(":type", "SD");
        this.context.router.push(path);
        break;
      }
      case 'SR': {
        //费用明细报表，图形化
        let path = menuRoute.getRouteItem('travel-report-fee-report', 'key').url.replace(':id', item.id);
        this.context.router.push(path);
        break;
      }
      case 'RSR': {
        //合规明细报表，图形化
        let path = menuRoute.getRouteItem('travel-report-fee-detail-report', 'key').url.replace(':id', item.id);
        this.context.router.push(path);
        break;
      }
      case 'PEAR': {
        // 个人图形化报表
        let path = menuRoute.getRouteItem('personnel-fee-report', 'key').url.replace(':id', item.id);
        this.context.router.push(path);
        break;
      }
      default: {
        //默认报表类型 （大部分）
        let path = menuRoute.getRouteItem('travel-report', 'key').url.replace(':id', item.id);
        this.context.router.push(path);
      }
    }
  }
  //分页点击
  onChangePager = (_pagination, filters, sorter) => {
    let pagination = this.state.pagination;
    pagination.page = _pagination.current - 1;
    this.setState({
      pagination
    }, () => {
      this.getReportList();
    })
  };

  render() {
    const { reportList, columns, loading, pagination } = this.state;
    return (
      <div className="report-setting-wrap">
        <ResizeTable
          dataSource={reportList}
          columns={columns}
          pagination={pagination}
          size="middle"
          onRow={record => ({
            onClick: () => this.goToReportDetail(record)
          })}
          onChange={this.onChangePager}
          loading={loading}
          bordered/>
      </div>
    )
  }
}

ReportList.contextTypes = {
  router: React.PropTypes.object
};

function mapStateToProps(state) {
  return {
    organization: state.budget.organization,
    company: state.login.company,
    language: state.main.language,
    tenantMode: state.main.tenantMode,
  }
}

export default connect(mapStateToProps)(ReportList);
